package com.quyeying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quyeying.entity.TblBusinessLicenceEntity;
import com.quyeying.entity.TblMerchantAuthenticationEntity;
import com.quyeying.entity.TblMerchantEntity;
import com.quyeying.entity.TblMerchantLicenceEntity;
import com.quyeying.mapper.TblMerchantAuthenticationDao;
import com.quyeying.service.TblMerchantAuthenticationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quyeying.service.TblMerchantLicenceService;
import com.quyeying.service.TblMerchantService;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.stereotype.Service;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ReturnPageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* <p>
 * 商户认证表 服务实现类
 * </p>
*
* @author jll
* @since 2022-12-14
*/
@Service
public class TblMerchantAuthenticationServiceImpl extends ServiceImpl<TblMerchantAuthenticationDao, TblMerchantAuthenticationEntity> implements TblMerchantAuthenticationService {

    @Resource
    private TblMerchantLicenceService tblMerchantLicenceService;
    @Resource
    private TblMerchantService tblMerchantService;

    @Override
    public R findListByPage(Integer page, Integer pageCount){
        IPage<TblMerchantAuthenticationEntity> wherePage = new Page<>(page, pageCount);
        TblMerchantAuthenticationEntity where = new TblMerchantAuthenticationEntity();
        IPage<TblMerchantAuthenticationEntity> iPage = baseMapper.selectPage(wherePage, Wrappers.query(where));
        return R.success(ReturnPageData.fillingData(iPage));
    }

    @Override
    public R add(Map<String, Object> map){
        TblMerchantAuthenticationEntity tblMerchantAuthenticationEntity = new TblMerchantAuthenticationEntity();
        tblMerchantAuthenticationEntity.setCategoryId(Long.valueOf(map.get("categoryId").toString()));
        tblMerchantAuthenticationEntity.setBusinessLicense(map.get("businessLicense").toString());
        tblMerchantAuthenticationEntity.setLegalPerson(map.get("legalPerson").toString());
        tblMerchantAuthenticationEntity.setAuthorization(map.get("authorization").toString());
        tblMerchantAuthenticationEntity.setTrademarkCard(map.get("trademarkCard").toString());
        tblMerchantAuthenticationEntity.setMerchantId(Long.valueOf(map.get("merchant_id").toString()));
        Integer count = baseMapper.selectCount(new QueryWrapper<TblMerchantAuthenticationEntity>().eq("merchant_id", tblMerchantAuthenticationEntity.getMerchantId()));
        if (count>0){
            return R.error(ResultEnum.SYSTEM_ERROR,"请勿重复认证");
        }
        baseMapper.insert(tblMerchantAuthenticationEntity);
        //插入认证书
        List<Map<String,Object>> licence = (List<Map<String, Object>>) map.get("licence");
        licence.stream().forEach(item ->{
            TblMerchantLicenceEntity tblMerchantLicenceEntity = new TblMerchantLicenceEntity();
            tblMerchantLicenceEntity.setMerchantId(tblMerchantAuthenticationEntity.getId());
            tblMerchantLicenceEntity.setLicenceId(Long.valueOf(item.get("licenceId").toString()));
            tblMerchantLicenceEntity.setLicenceImage(item.get("licenceImage").toString());
            tblMerchantLicenceService.add(tblMerchantLicenceEntity);
        });
        //修改商户认证状态
        TblMerchantEntity tblMerchantEntity = new TblMerchantEntity();
        tblMerchantEntity.setId(tblMerchantAuthenticationEntity.getMerchantId());
        tblMerchantEntity.setType(1);
        tblMerchantService.updateById(tblMerchantEntity);
        return R.success(tblMerchantService.getById(tblMerchantAuthenticationEntity.getMerchantId()));
    }

    @Override
    public R delete(Long id){
        baseMapper.deleteById(id);
        return R.success();
    }

    @Override
    public R updateData(TblMerchantAuthenticationEntity tblMerchantAuthenticationEntity){
        baseMapper.updateById(tblMerchantAuthenticationEntity);
        return R.success();
    }

    @Override
    public R findById(Long id){
        TblMerchantAuthenticationEntity tblMerchantAuthenticationEntity = baseMapper.selectById(id);
        return R.success(tblMerchantAuthenticationEntity);
    }
}
